package com.whli.autumn.mall.web.controller.sale;

import com.github.pagehelper.PageInfo;
import com.whli.autumn.core.app.annotation.ApiVersion;
import com.whli.autumn.core.extend.controller.BaseController;
import com.whli.autumn.core.permission.annotation.RequirePermissions;
import com.whli.autumn.core.share.ResponseResult;
import com.whli.autumn.mall.web.controller.convert.sale.SaleOrderConvert;
import com.whli.autumn.mall.sale.domain.dto.saleorder.SaleOrderAddDTO;
import com.whli.autumn.mall.sale.domain.dto.saleorder.SaleOrderPageDTO;
import com.whli.autumn.mall.sale.domain.dto.saleorder.SaleOrderUpdateDTO;
import com.whli.autumn.mall.sale.domain.entity.SaleOrder;
import com.whli.autumn.mall.sale.service.ISaleOrderService;
import com.whli.autumn.mall.sale.domain.vo.saleorder.SaleOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>销售订单API</p>
 *
 * @author whli
 * @version 1.0.0
 * @since 2023-05-01 16:38
 */
@Api(tags = "sale-销售订单")
@ApiVersion
@RestController
@RequestMapping(value = "/sale/saleOrder")
public class SaleOrderController extends BaseController<SaleOrder, ISaleOrderService> {

    /**
     * <p>查询销售订单详情</p>
     *
     * @author whli
     * @since 2023-05-01 16:38
     * @param id 销售订单主键
     * @return
     */
    @ApiOperation(value = "查询销售订单", httpMethod = "GET")
    @ApiImplicitParam(value = "主键", name = "id", required = true)
    @RequirePermissions("sale:sale-order:view")
    @GetMapping
    public ResponseResult<SaleOrderVO> view(String id) {
        SaleOrder entity = service.getByPK(id);
        return ResponseResult.success(SaleOrderConvert.INSTANCE.domainToVO(entity));
    }

    /**
     * <p>分页查询销售订单</p>
     *
     * @author  whli
     * @since   2023-05-01 16:38
     * @param   entity 分页参数
     * @return
     */
    @ApiOperation(value = "分页查询销售订单", httpMethod = "GET")
    @RequirePermissions("sale:sale-order:list")
    @GetMapping("page")
    public ResponseResult<PageInfo<SaleOrderVO>> listByPage(SaleOrderPageDTO entity) {
        PageInfo<SaleOrderVO> page = service.listByPage(entity);
        return ResponseResult.success(page);
    }

    /**
     * <p>新增销售订单</p>
     *
     * @author  whli
     * @since   2023-05-01 16:38
     * @param   entity 新增参数
     * @return
     */
    @ApiOperation(value = "新增销售订单", httpMethod = "POST", consumes = "application/json")
    @RequirePermissions("sale:sale-order:add")
    @PostMapping
    public ResponseResult<String> save(@RequestBody @Valid SaleOrderAddDTO entity) {
        String id = service.save(SaleOrderConvert.INSTANCE.addToDomain(entity));
        return !StringUtils.isEmpty(id) ? ResponseResult.success("新增销售订单成功") : ResponseResult.fail("新增销售订单失败");
    }

    /**
     * <p>修改销售订单</p>
     *
     * @author  whli
     * @since   2023-05-01 16:38
     * @param   entity  修改参数
     * @return
     */
    @ApiOperation(value = "修改销售订单", httpMethod = "PUT", consumes = "application/json")
    @RequirePermissions("sale:sale-order:update")
    @PutMapping
    public ResponseResult<String> update(@RequestBody @Valid SaleOrderUpdateDTO entity) {
        String id = service.update(SaleOrderConvert.INSTANCE.updateToDomain(entity));
        return !StringUtils.isEmpty(id) ? ResponseResult.success("修改销售订单成功") : ResponseResult.fail("修改销售订单失败");
    }

    /**
     * <p>删除销售订单</p>
     *
     * @author  whli
     * @since   2023-05-01 16:38
     * @param   ids 主键集合 java.util.List<String>
     * @return
     */
    @ApiOperation(value = "删除销售订单", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "主键集合", name = "ids", required = true)
    })
    @RequirePermissions("sale:sale-order:delete")
    @DeleteMapping
    public ResponseResult<String> delete(@RequestBody List<String> ids) {
        boolean flag = service.deleteBatch(ids);
        return flag ? ResponseResult.success("删除销售订单成功") : ResponseResult.fail("删除销售订单失败");
    }

}
